package com.gf.pokemon.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.Callable;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.gf.pokemon.constants.IsInput;
import com.gf.pokemon.constants.Vmode;
import com.gf.pokemon.constants.VoteStatus;
import com.gf.pokemon.domain.Vote;
import com.gf.pokemon.domain.VoteMsg;
import com.gf.pokemon.domain.VoteOption;
import com.gf.pokemon.dto.VoteDto;
import com.gf.pokemon.dto.VoteMsgDto;
import com.gf.pokemon.dto.VoteOptionDto;
import com.gf.pokemon.mapper.VoteMapper;
import com.gf.pokemon.mapper.VoteMsgMapper;
import com.gf.pokemon.mapper.VoteOptionMapper;
import com.gf.pokemon.platform.exception.PokeErrorCode;
import com.gf.pokemon.platform.exception.PokeException;
import com.gf.pokemon.util.ConvertUtils;
import com.gf.pokemon.util.page.Page;
import com.gf.pokemon.util.page.PagedList;
import com.gf.pokemon.util.page.PaginationUtil;

@Service
public class VoteService {
		
	@Resource
	private VoteMapper voteMapper;
	
	@Resource
	private VoteMsgMapper voteMsgMapper;
	
	@Resource
	private VoteOptionMapper voteOptionMapper;
	
	@Resource
	private AuserService auserService;
	
	/**
	 * OA回调连接
	 */
	private final String oa_show_url = "/vote/show?voteId=";

	/**
	 * 保存所有投票信息
	 * @param voteDto
	 * @throws PokeException
	 */
	public void saveVote(VoteDto voteDto) throws PokeException{
		
		//保存vote对象
		validateVote(voteDto);
		Vote vote = ConvertUtils.convert(voteDto);
		vote.setStatus(VoteStatus.voting.getType().shortValue());
		vote.setCreateDate(Calendar.getInstance().getTime());
		voteMapper.insert(vote);
		Integer voteId = vote.getId();
		
		this.saveVoteOption(voteId, voteDto.getVoteOptionDtos());		
		this.savteVoteMsg(voteId, voteDto.getVoteMsgDtos());
		//发送OA请求
		auserService.sendCreateOARequest(voteDto.getTitle(), voteDto.getMemo(),
				oa_show_url + voteId, voteId.toString());
	}
	
	public void saveVoteOption(Integer voteId,List<VoteOptionDto> voteOptionDtos) throws PokeException{
		//保存voteOption对象
		validateVoteOption(voteOptionDtos);
		for(VoteOptionDto voteOptionDto:voteOptionDtos){
			VoteOption voteOption = ConvertUtils.convert(voteOptionDto);
			voteOption.setVoteId(voteId);
			voteOption.setCreateDate(Calendar.getInstance().getTime());
			voteOptionMapper.insert(voteOption);
		}
	}
	
	public void savteVoteMsg(Integer voteId, List<VoteMsgDto> voteMsgDtos) throws PokeException{
		validateVoteMsg(voteMsgDtos);
		if(CollectionUtils.isNotEmpty(voteMsgDtos)){
			for(VoteMsgDto voteMsgDto:voteMsgDtos){
				VoteMsg voteMsg = ConvertUtils.convert(voteMsgDto);
				voteMsg.setVoteId(voteId);
				voteMsg.setCreateDate(Calendar.getInstance().getTime());
				voteMsgMapper.insert(voteMsg);
			}
		}
	}
	
	/**
	 * 根据query条件分页查询结果
	 * @param query
	 * @param page
	 * @return
	 * @throws PokeException
	 */
	public PagedList<VoteDto> getVotePage(VoteDto query,Page page) throws PokeException{
		if(query==null||page==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"查询参数错误");
		}
		final Vote dbQuery = ConvertUtils.convert(query);
		PagedList<Vote> voteInCondition =  PaginationUtil.pagedQuery(page, new Callable<List<Vote>>() {            
            public List<Vote> call() throws Exception {
                return voteMapper.queryByCondition(dbQuery);
            }
        });
		
		List<VoteDto> dataList = new ArrayList<VoteDto>();
		for(Vote vote:voteInCondition.getList()){
			dataList.add(ConvertUtils.convert(vote));
		}
		return new PagedList<VoteDto>(voteInCondition.getPage(),dataList);
	}
	
	public VoteDto getVoteById(Integer voteId) throws PokeException{
		if(voteId==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"id为空!");
		}
		Vote vote = voteMapper.selectByPrimaryKey(voteId);
		if(vote==null){
			return null;
		}
		
		VoteDto voteDto = ConvertUtils.convert(vote);
		List<VoteOption> voteOptions = voteOptionMapper.findByVoteId(voteId);
		if(CollectionUtils.isEmpty(voteOptions)){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"选项不存在："+voteId);
		}
		voteDto.setVoteOptionDtos(ConvertUtils.convertOptionList(voteOptions));
		
		List<VoteMsg> voteMsgs = voteMsgMapper.findByVoteId(voteId);
		if(CollectionUtils.isNotEmpty(voteMsgs)){
			voteDto.setVoteMsgDtos(ConvertUtils.convertMsgList(voteMsgs));
		}	
		
		return voteDto;
	}
	
	private void validateVoteMsg(List<VoteMsgDto> voteMsgDtos) throws PokeException {
		if(CollectionUtils.isEmpty(voteMsgDtos)){
			return;
		}
		for(VoteMsgDto voteMsgDto:voteMsgDtos){
			if(StringUtils.isBlank(voteMsgDto.getMsg())){
				throw new PokeException(PokeErrorCode.INVALID_PARAM,"用户信息描述为空");
			}
		}
	}

	private void validateVoteOption(List<VoteOptionDto> voteOptionDtos) throws PokeException {
		if(CollectionUtils.isEmpty(voteOptionDtos)){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"选项为空");
		}
		
		for(VoteOptionDto voteOptionDto:voteOptionDtos){
			if(voteOptionDto==null){
				throw new PokeException(PokeErrorCode.INVALID_PARAM,"单个选项为空");
			}
			if(StringUtils.isBlank(voteOptionDto.getOptionstr())){
				throw new PokeException(PokeErrorCode.INVALID_PARAM,"选项说明为空");
			}
			if(voteOptionDto.getIsinput() == null){
				throw new PokeException(PokeErrorCode.INVALID_PARAM,"是否输入数字参数为空");
			}
			
			IsInput.parse(Integer.valueOf(voteOptionDto.getIsinput()));
		}
	}

	private void validateVote(VoteDto voteDto) throws PokeException{
		if(voteDto==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"数据对象为空");
		}
		
		if(StringUtils.isBlank(voteDto.getTitle())){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"标题为空");
		}
		
		if(StringUtils.isBlank(voteDto.getTheme())){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"主题为空");
		}
		
		if(StringUtils.isBlank(voteDto.getOverDateStr())){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"截止时间为空");
		}
		
		if(voteDto.getVmode()==null ){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"选项模式为空");
		}
		
		Vmode.parse(Integer.valueOf(voteDto.getVmode()));
		
		if(voteDto.getOverDate().before(Calendar.getInstance().getTime())){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"截止时间比当前时间晚");
		}
	}

	public VoteMapper getVoteMapper() {
		return voteMapper;
	}

	public void setVoteMapper(VoteMapper voteMapper) {
		this.voteMapper = voteMapper;
	}

	public VoteMsgMapper getVoteMsgMapper() {
		return voteMsgMapper;
	}

	public void setVoteMsgMapper(VoteMsgMapper voteMsgMapper) {
		this.voteMsgMapper = voteMsgMapper;
	}

	public VoteOptionMapper getVoteOptionMapper() {
		return voteOptionMapper;
	}

	public void setVoteOptionMapper(VoteOptionMapper voteOptionMapper) {
		this.voteOptionMapper = voteOptionMapper;
	}
	
}
